package com.saleoffline.server.domain.sale;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.saleoffline.server.BaseController;
import com.saleoffline.server.PageNavigator;
import com.saleoffline.server.SaleException;
import com.saleoffline.server.Thumbnail;

@Controller
@RequestMapping("/sale")
public class SaleController extends BaseController {
	@RequestMapping("/form")
	public ModelAndView form(@RequestParam(defaultValue="0") int id) {
		Sale sale = saleDAO.select(id);
		ModelAndView mav = new ModelAndView("sale/form");
		mav.addObject("sale", sale);
		return mav;
	}

	@RequestMapping("/insert")
	public ModelAndView insert(Sale sale, HttpServletRequest request) throws Exception {
		savePhoto(sale, request);
		int id = saleDAO.insert(sale);
		ModelAndView mav = new ModelAndView(new RedirectView(getRedirectUrl("/sale/view")));
		mav.addObject("id", id);
		return mav;
	}

	@RequestMapping("/update")
	public ModelAndView update(Sale sale, HttpServletRequest request) throws Exception {
		savePhoto(sale, request);
		saleDAO.update(sale);
		ModelAndView mav = new ModelAndView(new RedirectView(getRedirectUrl("/sale/view")));
		mav.addObject("id", sale.getId());
		return mav;
	}
	
	@RequestMapping("/delete")
	public ModelAndView delete(int id) {
		saleDAO.delete(id);
		ModelAndView mav = new ModelAndView(new RedirectView(getRedirectUrl("/sale/list")));
		return mav;
	}
	
	@RequestMapping("/view")
	public ModelAndView view(int id) {
		Sale sale = saleDAO.select(id);
		if (sale == null) {
			throw new SaleException("message");
		}
		ModelAndView mav = new ModelAndView("sale/view");
		mav.addObject("sale", sale);
		return mav;
	}	
	
	@RequestMapping("/list")
	public ModelAndView list(SaleSearch search, PageNavigator pageNavigator) {
		search.setOffset(pageNavigator.getOffset());
		search.setLimit(pageNavigator.getListSize());
		List<Sale> saleList = saleDAO.search(search);
		
		ModelAndView mav = new ModelAndView("sale/list");
		mav.addObject("search", search);
		mav.addObject("pageNavigator", pageNavigator);
		mav.addObject("saleList", saleList);
		return mav;
	}
	
	private void savePhoto(Sale sale, HttpServletRequest request) throws IOException {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		List<MultipartFile> files = multipartRequest.getFiles("photos");
		
		if (files != null) {
			for (MultipartFile mFile : files) {
				String fileName = System.currentTimeMillis() + "_" + mFile.getOriginalFilename();
				String thumFileName = System.currentTimeMillis() + "_thumbnail_" + mFile.getOriginalFilename();
				File orgFile = new File("d:/dev/storage/photo/" + fileName);
				File thumFile = new File("d:/dev/storage/photo/" + thumFileName);
				FileUtils.writeByteArrayToFile(new File("d:/dev/storage/photo/" + fileName), mFile.getBytes());
				Thumbnail.createThumb(orgFile, thumFile, 90, 70);
				sale.setPhotoUrl1("http://jarang.iptime.org/photo/" + fileName);
				sale.setPhotoUrl2("http://jarang.iptime.org/photo/" + thumFileName);
			}
		}
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
	  binder.initBeanPropertyAccess();
	  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	  dateFormat.setLenient(false);
	  binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}	
}
